package com.seednia.module.core.profile.dto;

import org.apache.commons.lang.StringUtils;

import com.seednia.foundation.framework.dto.Constants;
import com.seednia.foundation.framework.dto.ICriteria;
import com.seednia.foundation.framework.dto.QueryParams;
import com.seednia.foundation.framework.util.NumberUtils;
import com.seednia.module.core.search.util.AbstractQueryBuilder;

public class ProfileQueryBuilder extends AbstractQueryBuilder {

	private String operatorStr = " OR";
	
	public ProfileQueryBuilder(QueryParams.Operator operator)
	{
		if (operator == QueryParams.Operator.AND) {
			operatorStr = " AND";
		}
	}
	
	@Override
	public String buildQueryString(ICriteria record) {
		ProfileCriteria criteria = (ProfileCriteria)record;
		StringBuilder retval = new StringBuilder();
		
		// OR-ed
		if ( StringUtils.isNotBlank(criteria.getNameLike()) ) {
			retval.append("(");
			retval.append("firstName:(").append(AbstractQueryBuilder.escape(criteria.getNameLike())).append(")");
			retval.append(" OR");
			retval.append(" lastName:(").append(AbstractQueryBuilder.escape(criteria.getNameLike())).append(")");
			retval.append(" OR");
			retval.append(" lctName:(").append(AbstractQueryBuilder.escape(criteria.getNameLike())).append(")");	
			retval.append(") ");
		} else {
			if ( StringUtils.isNotBlank(criteria.getCreatorDto().getFirstName()) ) {
				retval.append("firstName:(").append(AbstractQueryBuilder.escape(criteria.getCreatorDto().getFirstName())).append(")");
			}
			if ( StringUtils.isNotBlank(criteria.getCreatorDto().getLastName()) ) {
				if (retval.length() > 1) retval.append(operatorStr);
				retval.append(" lastName:(").append(AbstractQueryBuilder.escape(criteria.getCreatorDto().getLastName())).append(")");
			}
			if ( StringUtils.isNotBlank(criteria.getCreatorDto().getLcName()) ) {
				if (retval.length() > 1) retval.append(operatorStr);
				retval.append(" lctName:(").append(AbstractQueryBuilder.escape(criteria.getCreatorDto().getLcName())).append(")");			
			}
		}
		if ( StringUtils.isNotBlank(criteria.getCurrentLocation()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" currentLocation:(").append(AbstractQueryBuilder.escape(criteria.getCurrentLocation())).append(")");			
		}
		if ( StringUtils.isNotBlank(criteria.getGender()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" gender:(").append(AbstractQueryBuilder.escape(criteria.getGender())).append(")");			
		}
		if ( StringUtils.isNotBlank(criteria.getGoals()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" goals:(").append(AbstractQueryBuilder.escape(criteria.getGoals())).append(")");			
		}

		if ( StringUtils.isNotBlank(criteria.getHighlight()) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			retval.append(" highlight:(").append(AbstractQueryBuilder.escape(criteria.getHighlight())).append(")");
		}
		// OR all the tags
		String industryTagNames[] = criteria.getIndustryTagNames();
		if ( (industryTagNames != null) && (industryTagNames.length > 0 ) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			
			retval.append(" industryTagNamesCsv:(\"").append(AbstractQueryBuilder.escape(industryTagNames[0])).append("\"");
			for (int i=1; i < industryTagNames.length; i++) {
				retval.append(" OR ").append(AbstractQueryBuilder.escape(industryTagNames[i]));
			}
			retval.append(")");
		}
		String skillTagNames[] = criteria.getSkillTagNames();
		if ( (skillTagNames != null) && (skillTagNames.length > 0 ) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			
			retval.append(" skillTagNamesCsv:(\"").append(AbstractQueryBuilder.escape(skillTagNames[0])).append("\"");
			for (int i=1; i < skillTagNames.length; i++) {
				retval.append(" OR ").append(AbstractQueryBuilder.escape(skillTagNames[i]));
			}
			retval.append(")");
		}
		String interestTagNames[] = criteria.getInterestTagNames();
		if ( (interestTagNames != null) && (interestTagNames.length > 0 ) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			
			retval.append(" interestTagNamesCsv:(").append(AbstractQueryBuilder.escape(interestTagNames[0]));
			for (int i=1; i < interestTagNames.length; i++) {
				retval.append(" OR ").append(AbstractQueryBuilder.escape(interestTagNames[i]));
			}
			retval.append(")");
		}
		String lnguageTagNames[] = criteria.getLanguageTagNames();
		if ( (lnguageTagNames != null) && (lnguageTagNames.length > 0 ) ) {
			if (retval.length() > 1) retval.append(operatorStr);
			
			retval.append(" languageTagNamesCsv:(").append(AbstractQueryBuilder.escape(lnguageTagNames[0]));
			for (int i=1; i < lnguageTagNames.length; i++) {
				retval.append(" OR ").append(AbstractQueryBuilder.escape(lnguageTagNames[i]));
			}
			retval.append(")");
		}

		// Enclose in parenthesis so we can AND them 
		if (retval.length() > 1) {
			retval.insert(0, "(").append(")");
		}

		if ( criteria.getStatus() > Constants.INT_ANY ) {
			if (retval.length() > 1) retval.append(" AND");
			retval.append(" status:\"").append(criteria.getStatus()).append("\"");
		}
		if ( criteria.getTimeCommitment() > Constants.INT_ANY ) {
			if (retval.length() > 1) retval.append(" AND");
			retval.append(" timeCommitment:[").append(criteria.getTimeCommitment()).append(" TO 24]");
		}
		if ( criteria.getWantTo() > Constants.INT_ANY ) {
			// TODO: into or of bits
			Integer[] bitSetInts = NumberUtils.possibleBitSetIntegers(15, criteria.getWantTo());
			if (bitSetInts != null && bitSetInts.length > 0) {
				if (retval.length() > 1) retval.append(" AND");
				retval.append( " wantTo:(");
				for (int i=0; i < bitSetInts.length; i++) {
					if (i != 0)
						retval.append(" OR ");
					retval.append(bitSetInts[i]);
				}
				retval.append(")");
			}
		}
		if (retval.length() > 1) retval.append(" AND");
		retval.append(" privacyLevel:[0 TO ").append(criteria.getPrivacyLevel()).append("]");
		
		return retval.toString();
	}

}
